using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GUI_Webpage_Restaurant : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            DeleteBlank();
        }
    }

    protected void grvRestaurant_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvRestaurant.EditIndex = -1;
        DeleteBlank();
        LoadData();
    }
    protected void grvRestaurant_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvRestaurant.EditIndex = (int)e.NewEditIndex;
        LoadData();
    }
    protected void grvRestaurant_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        RestaurantInfo resInfo = new RestaurantInfo();
        RestaurantBll resBll = new RestaurantBll();

        resInfo.ResID = ((TextBox)grvRestaurant.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        resInfo.ResName = ((TextBox)grvRestaurant.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        resInfo.Phone = ((TextBox)grvRestaurant.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        resInfo.Address = ((TextBox)grvRestaurant.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        resInfo.ResDescription = ((TextBox)grvRestaurant.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

        grvRestaurant.EditIndex = -1;

        resBll.UpdateRestaurant(resInfo);
        DeleteBlank();
        LoadData();
    }
    protected void grvRestaurant_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        RestaurantInfo resInfo = new RestaurantInfo();
        RestaurantBll resBll = new RestaurantBll();

        resInfo.ResID = grvRestaurant.DataKeys[e.RowIndex].Value.ToString();
        resBll.DeleteRestaurant(resInfo);
        DeleteBlank();
        LoadData();
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        RestaurantInfo resInfo = new RestaurantInfo();
        RestaurantBll resBll = new RestaurantBll();

        resInfo.ResID = txtResID.Text;
        resInfo.ResName = txtResName.Text;
        resInfo.Phone = txtPhone.Text;
        resInfo.Address = txtAddress.Text;
        resInfo.ResPicture = fileRestaurant.FileName;
        resInfo.ResDescription = txtDescription.Text;

        if (resBll.createRestaurant(resInfo) >0)
        {
            LoadData();
            DeleteBlank();
        }
    }

    #region Load restaurant data to gridview
    private void LoadData()
    {
        RestaurantBll resBll = new RestaurantBll();
        try
        {
            SqlDataReader reader = resBll.ListAllRestaurant();
            grvRestaurant.DataSource = reader;
            grvRestaurant.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    #endregion

    #region Clear all control
    private void DeleteBlank()
    {
        txtResID.Text = "";
        txtResName.Text = "";
        txtPhone.Text = "";
        txtAddress.Text = "";
        txtDescription.Text = "";
    }
    #endregion
}
